import 'package:flutter/material.dart';
import '../../components/toast_utils.dart';

class FieldDialog extends StatefulWidget {
  final String title;
  final String? description;
  final String leftButtonText;
  final String rightButtonText;
  final VoidCallback? onLeftPressed;
  final ValueChanged<String>? onRightPressed;

  const FieldDialog({
    super.key,
    required this.title,
    this.description,
    required this.leftButtonText,
    required this.rightButtonText,
    this.onLeftPressed,
    this.onRightPressed,
  });

  @override
  State<FieldDialog> createState() => _FieldDialogState();
}

class _FieldDialogState extends State<FieldDialog> {
  late TextEditingController _controller;

  @override
  void initState() {
    super.initState();
    _controller = TextEditingController();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

@override
Widget build(BuildContext context) {
  return Material( // 添加这一层
    color: Colors.transparent, // 透明背景
    child: Stack(
      children: [
        Positioned.fill(
          child: Container(
            color: Colors.black.withValues(alpha: 0.5), // 这里应使用 withOpacity 而非 withValues
          ),
        ),
        Center(
          child: Container(
            width: 345,
            padding: const EdgeInsets.symmetric(vertical: 20, horizontal: 25),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(20),
            ),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                const SizedBox(height: 15),
                Text(
                  widget.title,
                  style: const TextStyle(
                    fontSize: 17,
                    color: Color(0xFF000000),
                  ),
                  textAlign: TextAlign.center,
                ),
                const SizedBox(height: 25),
                if (widget.description != null)
                  Padding(
                    padding: const EdgeInsets.only(bottom: 25),
                    child: Container(
                      decoration: BoxDecoration(
                        color: const Color(0xFFF5F8FE),
                        borderRadius: BorderRadius.circular(12),
                      ),
                      padding: const EdgeInsets.all(12),
                      child: TextField(
                        controller: _controller,
                        decoration: InputDecoration(
                          border: InputBorder.none,
                          contentPadding: EdgeInsets.zero,
                          isDense: true,
                          hintText: widget.description,
                          hintStyle: TextStyle(color: Color(0xFF666666)),
                        ),
                        style: const TextStyle(
                          fontSize: 15,
                          color: Colors.black,
                        ),
                        textAlign: TextAlign.center,
                      ),
                    ),
                  ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    SizedBox(
                      width: 140,
                      height: 48,
                      child: ElevatedButton(
                        style: ElevatedButton.styleFrom(
                          backgroundColor: const Color(0xFFEDEFEF),
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(15),
                          ),
                        ),
                        onPressed: widget.onLeftPressed,
                        child: Text(
                          widget.leftButtonText,
                          style: const TextStyle(
                            fontSize: 15,
                            color: Color(0xFF505050),
                          ),
                        ),
                      ),
                    ),
                    const SizedBox(width: 15),
                    SizedBox(
                      width: 140,
                      height: 48,
                      child: ElevatedButton(
                        style: ElevatedButton.styleFrom(
                          backgroundColor: const Color(0xFF5D5FEE),
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(15),
                          ),
                        ),
                        onPressed: () {
                          final text = _controller.text;
                          if (!RegExp(r'^[a-zA-Z0-9]{6,}$').hasMatch(text)) {
                            ToastUtils.showCenterToast(context, "请输入正确的兑换码");
                            return;
                          }
                          if (widget.onRightPressed != null) widget.onRightPressed!(text);
                        },
                        child: Text(
                          widget.rightButtonText,
                          style: const TextStyle(
                            fontSize: 15,
                            color: Colors.white,
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ),
      ],
    ),
  );
}

}